/*
 * Dialog ini muncul setiap pemain ingin menjual aset
 * 
 */


package controller.permainan;

import helper.SearchEngine;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import model.board.*;
import model.players.*;

public class Jual_dialog extends javax.swing.JFrame{

    private Permainan2 parent;
    private Player player;
    String[] asetlist;

    /** Creates new form LoadGame */
    public Jual_dialog(Permainan2 _parent, java.awt.event.MouseEvent evt)
    {
        initComponents();
        parent = _parent;
        player = parent.G.current_plyr();
        this.setLocationRelativeTo(null);

        //nama & icon pemain
        playername_label.setText(player.getNama());
        playericon_label.setIcon(player.getLargeImage());
        //isi list dengan aset pemain
        LoadAssetList();

        if (evt != null)
        {
            javax.swing.JPanel source = (javax.swing.JPanel) evt.getSource();
            int i = 1;
            while ((i<=32)&& (source != parent.kotak_panel[i]))
            {
                i++;
            }
            if (source == parent.kotak_panel[i])
            {
                aset_list.setSelectedValue(parent.kotakname_textfield[i].getText(), true);
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        back_button = new javax.swing.JButton();
        sellasset_button = new javax.swing.JButton();
        plyrattr_panel = new javax.swing.JPanel();
        aset_pane = new javax.swing.JScrollPane();
        aset_list = new javax.swing.JList();
        playericon_label = new javax.swing.JLabel();
        playername_label = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        asetpic_label = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        asetattr_textarea = new javax.swing.JTextArea();
        sellhotel_button = new javax.swing.JButton();
        sellvilla_button = new javax.swing.JButton();
        jumvilla_spinner = new javax.swing.JSpinner();
        jumhotel_spinner = new javax.swing.JSpinner();
        warning_label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Load Game");

        back_button.setText("Cancel");
        back_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                back_buttonActionPerformed(evt);
            }
        });

        sellasset_button.setText("Sell Asset");
        sellasset_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sellasset_buttonActionPerformed(evt);
            }
        });

        plyrattr_panel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        aset_list.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        aset_list.setFont(new java.awt.Font("Hand Me Down S (BRK)", 0, 20)); // NOI18N
        aset_list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        aset_list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                aset_listValueChanged(evt);
            }
        });
        aset_pane.setViewportView(aset_list);

        playericon_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        playername_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        playername_label.setText("plyr_name");

        javax.swing.GroupLayout plyrattr_panelLayout = new javax.swing.GroupLayout(plyrattr_panel);
        plyrattr_panel.setLayout(plyrattr_panelLayout);
        plyrattr_panelLayout.setHorizontalGroup(
            plyrattr_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(plyrattr_panelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(plyrattr_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(playericon_label, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addComponent(playername_label, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(aset_pane, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        plyrattr_panelLayout.setVerticalGroup(
            plyrattr_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(plyrattr_panelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(plyrattr_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(plyrattr_panelLayout.createSequentialGroup()
                        .addComponent(playericon_label, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                        .addComponent(playername_label, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, plyrattr_panelLayout.createSequentialGroup()
                        .addComponent(aset_pane, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
                        .addContainerGap())))
        );

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED, null, java.awt.Color.white, null, null));

        asetpic_label.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
        asetpic_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        asetattr_textarea.setColumns(20);
        asetattr_textarea.setRows(5);
        jScrollPane1.setViewportView(asetattr_textarea);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(asetpic_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(asetpic_label, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        sellhotel_button.setText("Sell Hotel");
        sellhotel_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sellhotel_buttonActionPerformed(evt);
            }
        });

        sellvilla_button.setText("Sell Villa");
        sellvilla_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sellvilla_buttonActionPerformed(evt);
            }
        });

        warning_label.setFont(new java.awt.Font("Calibri", 0, 11));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(plyrattr_panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(back_button, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(76, 76, 76)
                        .addComponent(warning_label, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(sellasset_button, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sellhotel_button, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jumhotel_spinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sellvilla_button, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jumvilla_spinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(plyrattr_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(back_button, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(sellasset_button, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(warning_label, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(sellvilla_button)
                            .addComponent(jumvilla_spinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(sellhotel_button)
                            .addComponent(jumhotel_spinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LoadAssetList()
    {
        Player P = parent.G.Q.player[parent.G.urutan_current_plyr];
        asetlist = new String[P.jum_aset];
        for (int i =1; i<=P.jum_aset; i++)
        {
            asetlist[i-1] = parent.G.B[P.aset[i]].getNama();
        }

        aset_list.setModel(new javax.swing.AbstractListModel()
        {

            public int getSize()
            {
                return asetlist.length;
            }

            public Object getElementAt(int index)
            {
                return asetlist[index];
            }
        });
        this.aset_pane.setViewportView(this.aset_list);
        this.aset_pane.repaint();
       
    }

    public void AssignButton()
    {
        int idx = helper.SearchEngine.LokasionBoard(parent.G.B, aset_list.getSelectedValue().toString());
        //KONDISI : LOKASI PARIWISATA
        if (parent.G.B[idx].getTipe() ==2)
        {
            LokasiPariwisata LP = (LokasiPariwisata) parent.G.B[idx];

            sellvilla_button.setVisible(true);
            jumvilla_spinner.setVisible(true);
            sellhotel_button.setVisible(true);
            jumhotel_spinner.setVisible(true);

            //KONDISI : TIDAK BOLEH JUAL TANAH
            if (!LP.bolehJual_Tanah(parent.G.B, idx, parent.G.current_plyr()))
            {
                sellasset_button.setEnabled(false);
                warning_label.setText("Sell remaining villa/hotel first");
            }
            // KONDISI : BOLEH JUAL TANAH
            else
            {
                sellasset_button.setEnabled(true);
                warning_label.setText("");
            }
            //KONDISI : JUMLAH VILLA 0
            if (LP.getJum_villa() == 0)
            {
                sellvilla_button.setEnabled(false);
                jumvilla_spinner.setEnabled(false);
            }
            //KONDISI : JUMLAH VILLA TIDAK 0
            else
            {
                sellvilla_button.setEnabled(true);
                jumvilla_spinner.setEnabled(true);
            }
            //KONDISI : JUMLAH HOTEL 0
            if (LP.getJum_hotel() == 0)
            {
                sellhotel_button.setEnabled(false);
                jumhotel_spinner.setEnabled(false);
            }
            //KONDISI : JUMLAH HOTEL TIDAK 0
            else
            {
                sellhotel_button.setEnabled(true);
                jumhotel_spinner.setEnabled(true);
            }
        }
        //KONDISI : BUKAN LOKASI PARIWISATA
        else
        {
            sellvilla_button.setVisible(false);
            jumvilla_spinner.setVisible(false);
            sellhotel_button.setVisible(false);
            jumhotel_spinner.setVisible(false);
        }
    }


    private void back_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_back_buttonActionPerformed
        this.dispose();
        parent.AssignAsThisPlyr(parent.G.urutan_current_plyr);
        parent.act_panel.setVisible(true);
        parent.sell_button.setText("Sell Asset");
        parent.NotifWrite("You cancel the sale");
        parent.setVisible(true);
    }//GEN-LAST:event_back_buttonActionPerformed

    private void aset_listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_aset_listValueChanged
        AssignButton();
        int idx = helper.SearchEngine.LokasionBoard(parent.G.B, aset_list.getSelectedValue().toString());
        System.out.println(idx);
        asetpic_label.setIcon(SearchEngine.createImageIcon("iconLP"+idx+".jpg"));
        playername_label.setText(parent.G.current_plyr().getNama());
        playericon_label.setIcon(SearchEngine.createImageIcon(parent.G.current_plyr().getIkon()));
        asetattr_textarea.setText(parent.G.B[idx].PrintAttribute());

        if (parent.G.B[idx].getTipe() == 2)
        {
            LokasiPariwisata lok = (LokasiPariwisata)parent.G.B[idx];
            SpinnerModel model_villa = new SpinnerNumberModel(0, 0, lok.getJum_villa(), 1);
            SpinnerModel model_hotel = new SpinnerNumberModel(0, 0, lok.getJum_hotel(), 1);

            jumvilla_spinner.setModel(model_villa);
            jumhotel_spinner.setModel(model_hotel);
        }

}//GEN-LAST:event_aset_listValueChanged

    private void sellasset_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sellasset_buttonActionPerformed
        int idx = helper.SearchEngine.LokasionBoard(parent.G.B, aset_list.getSelectedValue().toString());
        Kotak soldKotak = parent.G.B[idx];
        Player seller = parent.G.current_plyr();

        //hapus dari attr pemain
        int idxaset = helper.SearchEngine.WhereThisLPisinYourAset(idx, seller);
        seller.aset[idxaset] = 0;
        helper.SearchEngine.Asset_Defragmenter(seller);
        //LP owner diset null
        soldKotak.setOwner(null);
        //uang bertambah
        seller.addUang(soldKotak.getHargajual());
        this.dispose();
        javax.swing.JOptionPane.showMessageDialog(this, "Successfully sold~");
        parent.HistoryWrite(seller.getNama()+" sells "+soldKotak.getNama());
        parent.AssignAsThisPlyr(parent.G.urutan_current_plyr);
        parent.act_panel.setVisible(true);
        parent.sell_button.setText("Sell Asset");
        parent.setVisible(true);

}//GEN-LAST:event_sellasset_buttonActionPerformed

    private void sellhotel_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sellhotel_buttonActionPerformed
        Player seller = parent.G.current_plyr();
        int idx = helper.SearchEngine.LokasionBoard(parent.G.B, aset_list.getSelectedValue().toString());
        LokasiPariwisata soldKotak = (LokasiPariwisata) parent.G.B[idx];
        int jum_soldhotel = (Integer)jumhotel_spinner.getValue();

        //mengurangi jumlah hotel
        soldKotak.setJumhotel(soldKotak.getJum_hotel() - jum_soldhotel);
        //nambah uang seller
        seller.addUang(jum_soldhotel*soldKotak.getHargajual_hotel());
        javax.swing.JOptionPane.showMessageDialog(this, jum_soldhotel+" are sold!");
        
        AssignButton();
        asetattr_textarea.setText(soldKotak.PrintAttribute());
}//GEN-LAST:event_sellhotel_buttonActionPerformed

    private void sellvilla_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sellvilla_buttonActionPerformed
        Player seller = parent.G.current_plyr();
        int idx = helper.SearchEngine.LokasionBoard(parent.G.B, aset_list.getSelectedValue().toString());
        LokasiPariwisata soldKotak = (LokasiPariwisata) parent.G.B[idx];
        int jum_soldvilla = (Integer)jumvilla_spinner.getValue();
        
        //mengurangi jumlah villa
        soldKotak.setJumvilla(soldKotak.getJum_villa() - jum_soldvilla);
        //nambah uang seller
        seller.addUang(jum_soldvilla*soldKotak.getHargajual_villa());
        javax.swing.JOptionPane.showMessageDialog(this, jum_soldvilla+" are sold!");

        AssignButton();
        asetattr_textarea.setText(soldKotak.PrintAttribute());
}//GEN-LAST:event_sellvilla_buttonActionPerformed

     public static void puts (String S)
    {
        System.out.println(S);
    }
     
    public static void main(String args[]) {

        final Permainan2 game = new Permainan2();
        game.G.Q.player[1].aset[1] = 2;
        game.G.Q.player[1].aset[2] = 3;
        game.G.Q.player[1].aset[3] = 10;
        game.G.Q.player[1].jum_aset = 3;
        game.G.Q.player[2].aset[1] = 11;
        game.G.Q.player[2].jum_aset = 1;
        game.G.urutan_current_plyr = 1;
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Jual_dialog(game, null).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList aset_list;
    private javax.swing.JScrollPane aset_pane;
    private javax.swing.JTextArea asetattr_textarea;
    private javax.swing.JLabel asetpic_label;
    private javax.swing.JButton back_button;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner jumhotel_spinner;
    private javax.swing.JSpinner jumvilla_spinner;
    private javax.swing.JLabel playericon_label;
    private javax.swing.JLabel playername_label;
    private javax.swing.JPanel plyrattr_panel;
    private javax.swing.JButton sellasset_button;
    private javax.swing.JButton sellhotel_button;
    private javax.swing.JButton sellvilla_button;
    private javax.swing.JLabel warning_label;
    // End of variables declaration//GEN-END:variables

}
